﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Net.Chirash.Twitter.TwitterClient;
using Net.Chirash.Twitter.TwitterDatabase;
using Net.Chirash.Twitter.TwitterDatabase.Statistics;

namespace Net.Chirash.Twitter.TweetZone.WindowComponents.ViewModels
{
    public class ChartContainerViewModel : Net.Chirash.Twitter.TweetZone.WindowComponents.ViewModels.TwitterContainerViewModel<ValueContainer>
    {
        #region Constructors

        public ChartContainerViewModel()
            : base()
        {
            this.TwitterCommand = TwitterCommandFactory.CreateLocalTwitterCommand();

            Year = -1;
            Month = -1;
            Day = -1;
            Hour = -1;
            GroupBy = "Year";
        }

        #endregion

        #region Public Properties

        #region public int Year { get; set; }
        private int _Year;
        public int Year
        {
            get { return _Year; }
            set { _Year = value; OnPropertyChanged("Year"); }
        }
        #endregion
        #region public int Month { get; set; }
        private int _Month;
        public int Month
        {
            get { return _Month; }
            set { _Month = value; OnPropertyChanged("Month"); }
        }
        #endregion
        #region public int Day { get; set; }
        private int _Day;
        public int Day
        {
            get { return _Day; }
            set { _Day = value; OnPropertyChanged("Day"); }
        }
        #endregion
        #region public int Hour { get; set; }
        private int _Hour;
        public int Hour
        {
            get { return _Hour; }
            set { _Hour = value; OnPropertyChanged("Hour"); }
        }
        #endregion
        #region public string GroupBy { get; set; }
        private string _GroupBy;
        public string GroupBy
        {
            get { return _GroupBy; }
            set { _GroupBy = value; OnPropertyChanged("GroupBy"); }
        }
        #endregion

        public override string ContainerTitle
        {
            get
            {
                return "Chart";
            }
        }

        #endregion

        #region Protected Methods

        protected override void UpdateDatabase(TwitterCaching caching, IEnumerable<ValueContainer> updatelist)
        {
            // do nothing
        }

        protected override void RefreshDataProviding(RefreshArgs<ValueContainer> e)
        {
            StatusStatCountArgs args = new StatusStatCountArgs();

            args.UserID = -1; //all tweets
            args.year = Year;
            args.month = Month;
            args.day = Day;
            args.hour = Hour;

            Enum.TryParse<StatusStatCountArgs.StatGrouping>(
                this.GroupBy,
                out args.GroupBy
            );

            TwitterStatistics stat = TwitterCommandFactory.CreateTwitterStatistics();
            var table = stat.GetStatusCount(args);
            var list = new List<ValueContainer>(table.Count());

            //init
            for (int i = GetFirstNumber(this.GroupBy); i <= GetLastNumber(this.GroupBy); i++)
                list.Add(new ValueContainer(i, 0));

            foreach (var item in table)
            {
                list.Find((cnt) => cnt.Index == item.Index).Value = item.Value;
                //switch (GroupBy)
                //{
                //    case "Year":
                //        list.Find(new Predicate<ValueContainer>((m) => m.Index == item.Year)).Value = item.Count; break;
                //    case "Month":
                //        list.Find(new Predicate<ValueContainer>((m) => m.Index == item.Month)).Value = item.Count; break;
                //    case "Day":
                //        list.Find(new Predicate<ValueContainer>((m) => m.Index == item.Day)).Value = item.Count; break;
                //    case "Hour":
                //        list.Find(new Predicate<ValueContainer>((m) => m.Index == item.Hour)).Value = item.Count; break;
                //    default:
                //        throw new NotImplementedException();

                //}
            }
            e.ResultEnumerable = list;

        }

        #endregion

        #region Private Grouping Methods

        public static int GetFirstNumber(string grouping)
        {
            switch (grouping)
            {
                case "Year":
                    return 2006;
                case "Month":
                    return 1;
                case "Day":
                    return 1;
                case "Hour":
                    return 0;
                default:
                    throw new NotImplementedException();
            }
        }

        public static int GetLastNumber(string grouping)
        {
            switch (grouping)
            {
                case "Year":
                    return DateTime.Today.Year;
                case "Month":
                    return 12;
                case "Day":
                    return 31;
                case "Hour":
                    return 23;
                default:
                    throw new NotImplementedException();
            }
        }

        #endregion


    }
}
